
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


%{

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "main.h"
#include "misc.h"
#include "libmooigraph.h"
#include "options.h"

char* command = NULL;
int commandlen = 0;

#undef YY_INPUT
#define YY_INPUT(buf,ret,maxsize)  (ret = my_input( buf, maxsize ))

static int my_input (char* buf, int maxsize);

/* filename on commandline */
char *option_filename = NULL;

/* do not include edge labels in the graph */
int option_no_edgelabels = 0;

/* do not include unconnected nodes in the graph */
int option_no_singlenodes = 1;

/* draw edge lines as spline lines */
int option_splines = 1;

/* fold all subgraphs initially */
int option_init_folded = 0;

/* unfold all subgraphs initially */
int option_init_unfolded = 0;

/* if set allow selfedges */
int option_selfedges = 1;

/* print libmooigraph debug on stdout */
int option_lmgdebug = 0;

/* print debug on stdout */
int option_debug = 0;

/* write html page with colortable */
int option_colortable = 0;

/* show node names */
int option_nodenames = 0;

/* compensate gcc data bugs */
int option_gccbug = 1;

/* */
static void print_version (void);
static void print_help (void);

%}

	/* for lint purists according to flex manual */
%option nounput

	/* suppress gcc warning input() defined but not used */
%option noinput

	/* input does not come from a tty. */
%option never-interactive

	/* no yywrap() at end of file */
%option noyywrap

	/* 8-bits scanner */
%option 8bit

nonsep [^ \n]
digit [0-9]
nondigit [^0-9\n ]

%s SWITCH
%s BADSWITCH

%%

<INITIAL>"--"		{ BEGIN SWITCH; }
<INITIAL>" "		{;}
\n			{ return 0; }
<INITIAL>[^-" "\n]{nonsep}*	{
			 if (option_filename == NULL) {
			  option_filename = malloc (strlen(optionstext) + 1);
			  strcpy (option_filename, optionstext);
			 }
			}
<SWITCH>"nogccbug"	{ option_gccbug = 0; BEGIN INITIAL; }
<SWITCH>"gccbug"	{ option_gccbug = 1; BEGIN INITIAL; }
<SWITCH>"noedgelabels"	{ option_no_edgelabels = 1; BEGIN INITIAL; }
<SWITCH>"edgelabels"	{ option_no_edgelabels = 0; BEGIN INITIAL; }
<SWITCH>"nosinglenodes"	{ option_no_singlenodes = 1; BEGIN INITIAL; }
<SWITCH>"singlenodes"	{ option_no_singlenodes = 0; BEGIN INITIAL; }
<SWITCH>"nosplines"	{ option_splines = 0; BEGIN INITIAL; }
<SWITCH>"splines"	{ option_splines = 1; BEGIN INITIAL; }
<SWITCH>"selfedges"	{ option_selfedges = 1; BEGIN INITIAL; }
<SWITCH>"noselfedges"	{ option_selfedges = 0; BEGIN INITIAL; }
<SWITCH>"folded"	{ option_init_folded = 1; BEGIN INITIAL; }
<SWITCH>"unfolded"	{ option_init_unfolded = 1; BEGIN INITIAL; }
<SWITCH>"colortable"	{ option_colortable = 1; BEGIN INITIAL; }
<SWITCH>"lmgdebug"	{ option_lmgdebug = 1; BEGIN INITIAL; }
<SWITCH>"debug"		{ option_debug = 1; BEGIN INITIAL; }
<SWITCH>"nodenames"	{ option_nodenames = 1; BEGIN INITIAL; }
<SWITCH>"version"	{ print_version (); }
<SWITCH>"help"		{ print_help (); }
<SWITCH>.		{ BEGIN BADSWITCH; fprintf (stderr, "unknown option '%s'\n", optionstext); print_help (); }
<BADSWITCH>{nonsep}*	{ BEGIN INITIAL; }

%%

void options (int argc, char *argv[])
{
 int i;
 int len;
 len = 0;
 for (i = 1; i < argc; i++)
 {
  len = len + strlen (argv[i]);
  len = len + 1;
 }
 len = len + 1;
 command = (char*) mgr_malloc (len+1);
 command[0] = '\0';
 for (i = 1; i < argc; i++)
 {
  strcat (command, argv[i]);
  strcat (command, " ");
 }
 strcat (command, "\n");
 commandlen = strlen (command);
 options_flex_debug = 0;
 /* parse the commandline options using lexer */
 optionslex();
 /* */
 if (option_debug) {
  printf("option_filename = %s;\n",option_filename);

  /* do not include edge labels in the graph */
  printf("option_no_edgelabels = %d;\n",option_no_edgelabels);

  /* do not include unconnected nodes in the graph */
  printf("option_no_singlenodes = %d;\n",option_no_singlenodes);

  /* draw edge lines as spline lines */
  printf("option_splines = %d;\n",option_splines);

  /* fold all subgraphs initially */
  printf ("option_init_folded = %d;\n",option_init_folded);

  /* unfold all subgraphs initially */
  printf ("option_init_unfolded = %d;\n",option_init_unfolded);

  /* if set allow selfedges */
  printf ("option_selfedges = %d;\n",option_selfedges);

  /* print libmooigraph debug on stdout */
  printf("option_lmgdebug = %d;\n",option_lmgdebug);

  /* print debug on stdout */
  printf("option_debug = %d;\n",option_debug);

  /* write html page with colortable */
  printf("option_colortable = %d;\n",option_colortable);

  /* show node names */
  printf("option_nodenames = %d;\n",option_nodenames);

  /* compensate gcc data bugs */
  printf("option_gccbug = %d;\n",option_gccbug);

 }
 optionslex_destroy ();
 return;
}

static int my_input (char* buf, int maxsize)
{
 int n = maxsize;
 if (commandlen < maxsize) {
   n = commandlen;
 }
 if (n > 0) {
   memcpy( buf, command, n );
   commandlen -= n;
   command += n;
 }
 return n;
}

static void print_version (void)
{
 printf ("mooigraph version %s\n",PACKAGE_VERSION);
 printf ("libmooigraph version %s\n",lmg_version ());
 exit (0);
 return;
}

static void print_help (void)
{
 printf ("mooigraph [--options] [inputfile]\n");
 printf("%s\n","input file can be graphviz dot file or mooigraph format file");
 printf("%s\n","options are one or more of these options:");
 printf("%s\n","--nogccbug      do not compensate gcc data bugs");
 printf("%s\n","--gccbug        compensate gcc data bugs in parsing");
 printf("%s\n","--noedgelabels  do not draw edge labels");
 printf("%s\n","--edgelabels    do draw edge labels");
 printf("%s\n","--nosinglenodes do not draw unconnected single nodes");
 printf("%s\n","--singlenodes   do draw unconnected single nodes");
 printf("%s\n","--nosplines     draw normal edge lines");
 printf("%s\n","--splines       draw spline edge lines");
 printf("%s\n","--selfedges     do draw self edges");
 printf("%s\n","--noselfedges   do not draw self edges");
 printf("%s\n","--folded        fold all subgraphs initially");
 printf("%s\n","--unfolded      unfold all subgraphs initially");
 printf("%s\n","--colortable    generate html page with table of color names");
 printf("%s\n","--lmgdebug      print libmooigraph debug info to stdout");
 printf("%s\n","--debug         print debug info to stdout");
 printf("%s\n","--version       print version info");
 printf("%s\n","--help          print this option help");

 exit (0);
 return;
}

/* End */
